home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / CModalDialog C++ Class / CModalDialogClass.v100.sit / CModalDialog Class / CModalDialog.cp next >
Text File  |  1995-10-11  |  6KB  |  278 lines

  1. // CModalDialog.cp
  2. //     Copyright ⌐ 1995 by Michael F. Kamprath, All rights reserved.
  3. //
  4. // Contact information:
  5. //     mailto:kamprat@leonardo.net
  6. //     http://www.leonardo.net/kamprath
  7. //
  8. // License:
  9. //     This code may be freely used in free and shareware products. Commercial product
  10. //     users must supply me with a free, fully licensed copy of the product this code is
  11. //     used in. In either case, users should notify me of their use of this code.
  12. //
  13. //     This code may be freely distributed in it's non-modified form. It's original archive
  14. //     should be kept intact.
  15. //
  16. // Version History:
  17. //     v1.0.0 - Inititial release.
  18. //
  19.  
  20. #include "CModalDialog.h"
  21.  
  22. pascal Boolean    CModalDialogFilter(    DialogPtr theDlg,
  23.                                         EventRecord *theEvent, 
  24.                                         short *itemHit);
  25.  
  26. CModalDialog::CModalDialog(     short    resID,
  27.                                 short    defaultID,
  28.                                 short    cancelID )
  29.     :    dlogResID( resID ),
  30.         defaultItem( defaultID ),
  31.         cancelItem( cancelID ),
  32.         showHasBeenCalled( false )
  33. {
  34.     this->dialogFilterUPP = NewModalFilterProc( CModalDialogFilter );
  35.     this->theDialog = ::GetNewDialog(resID, nil, (WindowPtr)-1L );
  36.     SetWRefCon( this->theDialog, (long)this );
  37. }
  38.  
  39. CModalDialog::~CModalDialog()
  40. {
  41.     DisposeRoutineDescriptor((UniversalProcPtr)this->dialogFilterUPP);
  42.     ::DisposeDialog( this->theDialog );
  43. }
  44.  
  45. void
  46. CModalDialog::SetDialogPort( void )
  47. {
  48.     ::SetPort( this->theDialog );
  49. }
  50. void
  51. CModalDialog::SetDefaultItem( short newVal )
  52. {
  53.     this->InvalidateDefaultRect();
  54.     this->defaultItem = newVal;
  55.     this->InvalidateDefaultRect();
  56. }
  57. void
  58. CModalDialog::SetCancelItem( short newVal )
  59. {
  60.     this->cancelItem = newVal;
  61. }
  62.     
  63. void
  64. CModalDialog::GetItemInfo( short item, short* itemType, Handle* itemHandle, Rect* itemRect )
  65. {
  66.     ::GetDialogItem(this->theDialog, item,itemType, itemHandle,itemRect);
  67. }
  68. void
  69. CModalDialog::SetItemInfo( short item, short itemType, Handle itemHandle, Rect* itemRect )
  70. {
  71.     ::SetDialogItem(this->theDialog, item,itemType, itemHandle,itemRect);
  72. }
  73.  
  74. void
  75. CModalDialog::FrameDefaultButton(void)
  76. {
  77. GrafPtr            savePort;
  78. PenState        thePnState;
  79. Rect            defaultRect;
  80.  
  81.     this->GetDefaultRect( defaultRect );
  82.     
  83.     ::GetPort(&savePort);
  84.     ::SetPort(this->theDialog);
  85.     ::GetPenState( &thePnState );
  86.     
  87.     ::PenSize(3,3);
  88.     
  89.     ::FrameRoundRect(&defaultRect, 18, 18);
  90.     
  91.     ::SetPenState( &thePnState );
  92.     ::SetPort(savePort);
  93. }
  94.  
  95. void
  96. CModalDialog::GetDefaultRect( Rect& theRect )
  97. {
  98. short            itemType;
  99. Rect            itemRect;
  100. Handle            itemHandle;
  101.  
  102.     ::GetDialogItem( this->theDialog, this->defaultItem, &itemType, &itemHandle, &itemRect);
  103.  
  104.     itemRect.top -= 4;
  105.     itemRect.left -= 4;
  106.     itemRect.right += 4;
  107.     itemRect.bottom += 4;
  108.  
  109.     theRect = itemRect;
  110. }
  111. void
  112. CModalDialog::InvalidateDefaultRect(void)
  113. {
  114. Rect            defaultRect;
  115. GrafPtr            savePort;
  116.  
  117.     this->GetDefaultRect( defaultRect );
  118.     ::GetPort(&savePort);
  119.     ::SetPort(this->theDialog);
  120.     ::InvalRect(&defaultRect);
  121.     ::SetPort(savePort);
  122. }
  123.  
  124. void
  125. CModalDialog::FakeDefaultButtonPress( void )
  126. {
  127. short            itemType;
  128. Rect            itemRect;
  129. Handle            itemHandle;
  130. long            ticks = ::TickCount();
  131.  
  132.     ::GetDialogItem( this->theDialog, this->defaultItem, &itemType, &itemHandle, &itemRect);
  133.  
  134.     ::HiliteControl( (ControlHandle)itemHandle, 1 );
  135.     while (ticks + 10 > ::TickCount() )
  136.         ;
  137.     ::HiliteControl( (ControlHandle)itemHandle, 0 );
  138. }
  139.  
  140. void
  141. CModalDialog::FakeCancelButtonPress( void )
  142. {
  143. short            itemType;
  144. Rect            itemRect;
  145. Handle            itemHandle;
  146. long            ticks = ::TickCount();
  147.  
  148.     ::GetDialogItem( this->theDialog, this->cancelItem, &itemType, &itemHandle, &itemRect);
  149.  
  150.     ::HiliteControl( (ControlHandle)itemHandle, 1 );
  151.     while (ticks + 10 > ::TickCount() )
  152.         ;
  153.     ::HiliteControl( (ControlHandle)itemHandle, 0 );
  154. }
  155.  
  156. void
  157. CModalDialog::Show( void )
  158. {
  159.     ::ShowWindow(this->theDialog);
  160.     ::SelectWindow(this->theDialog);
  161.     
  162.     this->showHasBeenCalled = true;
  163. }
  164. void
  165. CModalDialog::Hide( void )
  166. {
  167.     ::HideWindow(this->theDialog);
  168. }
  169.  
  170. short    
  171. CModalDialog::DoOneModalLoop( void )
  172. {
  173. short    itemHit;
  174.  
  175.     if (!this->showHasBeenCalled)
  176.         this->Show();
  177.     ::ModalDialog(this->dialogFilterUPP, &itemHit);
  178.     
  179.     return itemHit;
  180. }
  181.  
  182. //
  183. // This is the dialog filter stuff.
  184. //
  185.  
  186. Boolean
  187. CModalDialog::UpdateFilter( DialogPtr theDlg, EventRecord *theEvent, short *itemHit)
  188. {
  189. GrafPtr            savePort;
  190. Boolean            returnCode = false;
  191.  
  192.     if ( (WindowPtr)(theEvent->message) == theDlg  )
  193.     {
  194.         GetPort(&savePort);
  195.         SetPort(theDlg);
  196.         BeginUpdate(theDlg);
  197.                 
  198.         this->DrawDialog();
  199.         
  200.         EndUpdate(theDlg);
  201.         SetPort(savePort);
  202.     
  203.         *itemHit = 0;
  204.         returnCode = true;
  205.     }
  206.     return returnCode;
  207. }
  208.  
  209. void
  210. CModalDialog::DrawDialog( void )
  211. {
  212.     ::UpdateDialog(this->theDialog,this->theDialog->visRgn);
  213.                     
  214.     this->FrameDefaultButton();
  215. }
  216.  
  217. Boolean
  218. CModalDialog::KeyDownFilter( DialogPtr /* theDlg */, EventRecord *theEvent, short *itemHit)
  219. {
  220. Boolean        returnCode = false;
  221. char        key = theEvent->message&charCodeMask;
  222.     
  223.     if (( key == 0x0D )||( key == 0x03)) 
  224.     // return or enter key
  225.     {
  226.         this->FakeDefaultButtonPress();
  227.         *itemHit = this->defaultItem;
  228.         returnCode = true;
  229.     }    
  230.     else if (    ( key == 0x1B )
  231.                 || (( key == '.')&&(theEvent->modifiers&cmdKey)) ) 
  232.     // cancel or command period
  233.     {
  234.         this->FakeCancelButtonPress();
  235.         *itemHit = this->cancelItem;
  236.         returnCode = true;
  237.     }
  238.     return returnCode;
  239. }
  240. Boolean
  241. CModalDialog::MouseDownFilter( DialogPtr, EventRecord*, short* )
  242. {
  243.     return false;
  244. }
  245. Boolean
  246. CModalDialog::OtherFilter( DialogPtr, EventRecord*, short* )
  247. {
  248.     return false;
  249. }
  250.  
  251. pascal Boolean CModalDialogFilter( DialogPtr theDlg, EventRecord *theEvent, short *itemHit)
  252. {
  253. CModalDialog    *theThis;
  254. Boolean            returnCode;
  255.  
  256.     theThis = (CModalDialog*)GetWRefCon(theDlg);
  257.     
  258.     if (theDlg != theThis->theDialog)
  259.         return false;
  260.             
  261.     switch ( theEvent->what )
  262.     {
  263.         case updateEvt:
  264.             returnCode = theThis->UpdateFilter( theDlg, theEvent, itemHit );
  265.             break;
  266.         case keyDown:
  267.             returnCode = theThis->KeyDownFilter( theDlg, theEvent, itemHit );
  268.             break;
  269.         case mouseDown:
  270.             returnCode = theThis->MouseDownFilter( theDlg, theEvent, itemHit );
  271.             break;
  272.         default:
  273.             returnCode = theThis->OtherFilter( theDlg, theEvent, itemHit );
  274.             break;
  275.     }
  276.     return (returnCode);
  277. }
  278.